<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
     <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="querInfo.query"   clearable @clear="getGoodsList">
              <el-button slot="append" icon="el-icon-search" @click="getGoodsList" ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddpage">添加商品</el-button>
        </el-col>
      </el-row>

     <el-table :data="goodsList" stripe border>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="商品名称"  prop="goods_name" ></el-table-column>
        <el-table-column label="商品价格" prop="goods_price" width="95px" ></el-table-column>
        <el-table-column label="商品重量"  prop="goods_weight" width="70px"></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="145px">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" prop="" width="130px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="editGoods(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeGoods(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="querInfo.pagenum"
            :page-sizes="[5, 10, 30]"
            :page-size="querInfo.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>

    </el-card>

   <el-dialog title="编辑商品" :visible.sync="editGoodsDialogVisible"  width="60%" >
     <el-form :model="goodsInfo" status-icon  ref="ruleForm" label-width="100px" class="demo-ruleForm">
       <el-form-item label="商品名称">
         <el-input v-model="goodsInfo.goods_name" placeholder="" width="150px"></el-input>
       </el-form-item>
       <el-form-item label="商品价格">
         <el-input v-model="goodsInfo.goods_price" placeholder="请输入角色描述" ></el-input>
       </el-form-item>
       <el-form-item label="商品数量">
         <el-input v-model.number="goodsInfo.goods_number" type="number" placeholder="请输入角色描述" ></el-input>
       </el-form-item>
       <el-form-item label="商品重量">
         <el-input v-model="goodsInfo.goods_weight" placeholder="请输入角色描述" ></el-input>
       </el-form-item>
       <el-form-item>
        <el-button @click="editGoodsDialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="saveGoodsInfo">确 定</el-button>
       </el-form-item>
     </el-form>
    </el-dialog>

  </div>
</template>

<script>
  export default{
    data() {
      return {
          editGoodsDialogVisible: false,
          goodsList: [ ],
          goodsInfo: {
            goods_id: '',
            goods_name: '',
            goods_price: '',
            goods_number: '',
            goods_weight: ''
          },
          querInfo:{
            query:'',
            pagenum:1,
            pagesize:5,
          },
          total:0
      }
    },
    created() {
      this.getGoodsList()
    },
    methods:{
        goAddpage(){
          this.$router.push('goods/add')
        },
        async saveGoodsInfo(){
          const id = this.goodsInfo.goods_id
            console.log(this.goodsInfo)
            const {data: res } = await this.$http.put('goods/' + id,{
                goods_name: this.goodsInfo.goods_name,
                goods_price:  this.goodsInfo.goods_price,
                goods_number: this.goodsInfo.goods_number,
                goods_weight: this.goodsInfo.goods_weight
        })
        console.log(res)

        // if(res.meta.status !== 201) return this.$message.error('更新商品失败!')
        // this.$message.success('更新商品成功!')
        // this.getGoodsList()

      },
      editGoods(goodsInfo) {

          this.editGoodsDialogVisible = true
          this.goodsInfo = goodsInfo
          console.log(this.goodsInfo)

      },
      async removeGoods(id) {
          const {data: res } = await this.$http.delete('goods/' + id)
          if(res.meta.status !== 200) return this.$message.error('删除商品失败!')
          this.$message.success('删除商品成功!')
          this.getGoodsList()
      },

      handleSizeChange(e){
          console.log(e)
          this.querInfo.pagesize = e
          this.getGoodsList()
      },
      handleCurrentChange(e){
        console.log(e)
        this.querInfo.pagenum = e
          this.getGoodsList()
      },
      async getGoodsList(){
        const { data: res } = await this.$http.get('goods',{params:this.querInfo})
        if(res.meta.status !== 200) return this.$message.error('获取菜单列表失败!')
        this.$message.success('获取商品列表成功')
        console.log(res.data)
        this.goodsList = res.data.goods
        this.total = res.data.total
      }
    }
  }
</script>

<style lang="less" scoped>

  lable{ margin-left: 10px;}

  .shortInput{
    .el-input{ width: 180px;}
    display: flex;
    justify-content: flex-start;
    text-align: left;
    div{
      margin-top:10px;
      pad: 5px;
      }
  }
  .el-input{
      margin-right: 10px;
  }
</style>
